package com.ztesoft.huangfu.learning.springboot.springboottransaction.service.impl;

import com.ztesoft.huangfu.learning.springboot.springboottransaction.dao.RoleMapper;
import com.ztesoft.huangfu.learning.springboot.springboottransaction.dao.UserDao;
import com.ztesoft.huangfu.learning.springboot.springboottransaction.entity.User;
import com.ztesoft.huangfu.learning.springboot.springboottransaction.error.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ztesoft.huangfu.learning.springboot.springboottransaction.error.Error.errMethod;
import static com.ztesoft.huangfu.learning.springboot.springboottransaction.error.Error.errMethod2;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/6/9
 * Time: 15:46
 * Version: 1.0
 * Description:事物
 **/
@Service
public class TransactionService {
    @Autowired
    private UserDao userDao;
    /**
     * 增删改要写 ReadOnly=false 为可写
     * @param user 用户
     */
    @Transactional(readOnly = false)
    public void updateUserError(User user) {
        userDao.update(user);
        errMethod(); // 执行一个会抛出异常的方法
    }

    @Transactional(readOnly = false, noRollbackFor = {MyException.class})
    public void updateUserError2(User user) {
        userDao.update(user);
        errMethod2();    }
}
